package com.fram.service.api.v1.impl;

import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fram.bean.api.collection.request.CollectionSearchRequest;
import com.fram.bean.api.collection.response.CollectionSearchResponse;
import com.fram.bean.sys.PageBean;
import com.fram.common.canstant.ErrCode;
import com.fram.common.exception.ProAffairException;
import com.fram.common.exception.ProException;
import com.fram.common.exception.ProRunTimeException;
import com.fram.common.exception.ProValiDataException;
import com.fram.entity.UserCollection;
import com.fram.mapper.UserCollectionMapper;
import com.fram.service.api.v1.UserCollectionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * @作者： 陳曉鬆 @时间： 2020年5月19日 - 上午9:36:42 @description：->
 */
@Service("collectionService")
public class UserCollectionServiceImpl implements UserCollectionService {

	@Autowired
	private UserCollectionMapper collectionMapper;

	@Override
	public void add(UserCollection collection) throws ProException {
		if (collectionMapper.selectBasic(collection) != null) {
			throw new ProValiDataException(ErrCode.ERR_HAVE_EXISTED.getReturnCode(), ErrCode.ERR_HAVE_EXISTED.getReturnMsg());
		}
		if (collectionMapper.insertSelective(collection) <= 0) {
			throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), "添加收藏失败,请稍后重试");
		}
	}

	@Override
	@Transactional
	public void delete(List<UserCollection> collection) throws ProAffairException {
		for (UserCollection c : collection) {
			if (collectionMapper.delete(c) <= 0) {
				throw new ProRunTimeException(ErrCode.FAIL.getReturnCode(), "删除收藏失败,请稍后重试");
			}
		}
	}

	@Override
	public PageInfo<CollectionSearchResponse> list(PageBean<CollectionSearchRequest> request) throws ProException {
		PageHelper.startPage(request.getPageNum(), request.getPageSize());
		List<CollectionSearchResponse> list = collectionMapper.list(request.getParam());
		if (list == null || list.size() == 0) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return new PageInfo<>(list);
	}

}
